function [feaScore, feaIdx] = fs_unsup_LGD(X, k)
if ~exist('k', 'var')
   k = 5;
end
[nSmp, nDim] = size(X);
a = sum( bsxfun(@minus, X, mean(X, 1)).^2); % 1 * nDim
b = zeros(1, nDim);
feaScore = zeros(nDim, 1);

for i = 1 : nDim    
	Mi = computeNeighbor(X, k, i); % nSmp * k
    for j = 1 : nSmp	    
        Aj = Mi(j, :);
        Xlocal = X(Aj', i); % k * 1
        b(i) = b(i) + sum( (Xlocal - mean(Xlocal)).^2);
    end
end
feaScore = a ./ b;
feaScore = feaScore';
[feaScore, feaIdx] = sort(feaScore, 'descend');
end



% function G = computeLocalVar(X, Aj, i1)
% [~, n] = size(Aj);
% f = 0;
% G = 0;
% for i = 1 : n
    % f = f + X(Aj(:,i), i1);
% end
% f = f/n;

% for i = 1 : n
% G = G + (X(Aj(:,i), i1)-f)^2;
% end	
	
% end




function M = computeNeighbor(X, k, i)
if ~exist('k', 'var')
   k = 5;
end
[nSmp,nDim] = size(X);
% FIND NEIGHBORS
X1= X(:, i);
dist = abs(bsxfun(@minus, X1, X1'));
%X2 = X1.^2;
%distance = repmat(X2',nSmp,1)+repmat(X2 ,1,nSmp)-2*(X1*X1');
[sorted,index] = sort(dist, 2);
M = index(:,2:(1+k)); 

end 